<template>
  <el-dialog
    :title="title"
    :visible="visible"
    :append-to-body="true"
    @close="handleDialogClose"
    width="600px"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-position="right"
      label-width="100px"
      class="form"
    >
      <el-form-item label="表单名称:" prop="formName">
        <el-input
          class="form-item"
          v-model="form.formName"
          placeholder="请输入表单名称"
        />
      </el-form-item>
      <el-form-item label="表单描述:" prop="description">
        <el-input
          class="form-item"
          v-model="form.description"
          placeholder="请输入表单描述"
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import FormDesignService from '../../../../service/form_design'

export default {
  props: {
    title: {
      type: String,
      required: true,
      default: () => ''
    },
    show: {
      type: Boolean,
      required: true,
      default: () => false
    },
    data: {
      type: Object,
      required: false,
      default: () => {}
    }
  },
  data() {
    // 表单名称验证
    const checkFormName = async (rule, value, callback) => {
      if (value.length === 0) {
        return callback(new Error('请输入表单名称'))
      } else {
        /*     const result = await this.getIsExistsByFormName(value)
        if (result.data) {
          return callback(new Error('该表单名称已被注册'))
        } */
        callback()
      }
    }

    return {
      visible: false,
      form: {
        formId: '',
        formName: '',
        description: ''
      },
      rules: {
        formName: {
          required: true,
          validator: checkFormName,
          trigger: 'blur'
        },
        description: {
          required: true,
          message: '请输入部门描述',
          trigger: 'blur'
        }
      }
    }
  },
  methods: {
    // 处理Dialog关闭事件
    handleDialogClose() {
      console.log(this.form)
      this.$emit('update:show')
      this.form = {
        formId: '',
        formName: '',
        description: ''
      }
      this.$refs.form.resetFields()
    },
    // 判断部门名称是否已被注册
    getIsExistsByFormName(formName) {
      return FormDesignService.getIsExistsByFormName(formName, this.form.formId)
    },
    // 提交表单
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.formId) {
            /*            FormDesignService.updateForm(this.form).then(res => {
              // 提示
              this.$message.success(res.message)
              // 关闭Dialog
              this.visible = false
              // 派发事件刷新Table
              this.$emit('success')
            }) */
            let formList = JSON.parse(localStorage.getItem('formList'))
            let index = formList.findIndex(
              el => el.formId === this.form.formId
            )
            console.log(formList)
            console.log(this.form)
            console.log(index)
            formList[index] = JSON.parse(JSON.stringify(this.form))
            localStorage.setItem('formList', JSON.stringify(formList))
            this.visible = false
            this.$emit('success')
          } else {
            /*  FormDesignService.insertForm(this.form).then(res => {
              // 提示
              this.$message.success(res.message)
              // 关闭Dialog
              this.visible = false
              // 派发事件刷新Table
              this.$emit('success')
            }) */
            console.log(22222222)
            this.form.formId = new Date().getTime()
            this.form.createTime = new Date().toDateString()
            let formList = JSON.parse(localStorage.getItem('formList'))
            formList = formList === null ? [] : formList
            formList.push(JSON.parse(JSON.stringify(this.form)))
            localStorage.setItem('formList', JSON.stringify(formList))
            this.visible = false
            this.$emit('success')
          }
        }
      })
    }
  },
  watch: {
    show(value) {
      this.visible = value
    },
    data(value) {
      this.form = value || {
        formId: '',
        formName: '',
        description: ''
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
.form
  margin 0 30px
  .form-item
    width 300px
</style>
